package com.ynsj.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.alicloud.sms.ISmsService;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.ynsj.domain.Config;
import com.ynsj.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynsj.mapper.SmsMapper;
import com.ynsj.domain.Sms;
import com.ynsj.service.SmsService;
@Service
@Slf4j
public class SmsServiceImpl extends ServiceImpl<SmsMapper, Sms> implements SmsService{

    @Autowired
    private ISmsService iSmsService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送一个短信
     *
     * @param sms 短信内容
     * @return 是否发送成功
     */
    @Override
    public boolean sendSms(Sms sms) {
        log.info("发送短信{}", JSON.toJSONString(sms,true));
        SendSmsRequest sendSmsRequest = buildSmsRequest(sms);
        try {
            SendSmsResponse sendSmsResponse = iSmsService.sendSmsRequest(sendSmsRequest);
            log.info("发送的结果为{}",JSON.toJSONString(sendSmsResponse,true));
            sms.setStatus(1);
            return save(sms);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 构建发送短信的请求对象
     * @param sms
     * @return
     */
    private SendSmsRequest buildSmsRequest(Sms sms) {
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(sms.getMobile());//发送给谁
        // Required:SMS-SignName-could be found in sms console
        Config signConfig = configService.getConfigByCode("SIGN");
        request.setSignName(signConfig.getValue());//设置签名----公司里的签名不会随意改变---config 查询签名
        // Required:Template-could be found in sms console
        Config configByCode = configService.getConfigByCode(sms.getTemplateCode());
        if (configByCode == null){
            throw new IllegalArgumentException("您输入的签名不存在");
        }
        request.setTemplateCode(configByCode.getValue());//模板code动态改变
        // Required:The param of sms template.For exmaple, if the template is "Hello,your verification code is ${code}". The param should be like following value
        String s = RandomUtil.randomNumbers(6);
        //需要把code保存至redis里面
        stringRedisTemplate.opsForValue().set("SMS:"+sms.getTemplateCode()+":"+sms.getMobile(),s,10,TimeUnit.SECONDS);
        request.setTemplateParam("{\"code\":\"" + s + "\"}");
        //设置短信内容
        String desc = configByCode.getDesc();
        String content = signConfig.getValue() +":"+ desc.replaceAll("\\$\\{code}",s);
        sms.setContent(content);
        return request;
    }
}
